package com.army.modules.batch.controller;


import com.army.common.properties.FileProperties;
import com.army.modules.batch.entity.HouseReq;
import com.army.modules.batch.job.DataTofile;
import com.army.modules.batch.mapper.ABusinessMapper;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.JobParametersBuilder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2021-12-20
 */
@Controller
@Slf4j
@AllArgsConstructor
public class ABusinessController {
    private final DataTofile dataTofile;
    private final FileProperties fileProperties;
    private final ABusinessMapper aBusinessMapper;
    @RequestMapping("/aBusiness")
    @ResponseBody
    public void FileToData(){
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        String fileName = sdf.format(date);
        //查询总笔数 总金额
        HouseReq houseReq = aBusinessMapper.sumTotal();
        //初始化任务参数
        JobParameters jobParameters = new JobParametersBuilder()
                .addString("fileName",fileProperties.getSendFilePath()+fileName+".txt")
                .addString("totalAmt",houseReq.getTotalAmt())
                .addString("totalCnt",houseReq.getTotalCnt())
                .toJobParameters();
        try {
            dataTofile.run(jobParameters);
            System.out.println("jobParameters = " + jobParameters);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
